package com.zhongke.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.zhongke.common.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Author ：wencj
 * Date： 2020/9/30
 * Description: 自定义TexTView带有Icon的,解决drawable不与文字居中的问题
 */
public class ZKDrawableCenterTextView extends AppCompatTextView {

    private Drawable[] drawables;
    private int[] drawableWidths;
    private int[] drawableHeights;

    public static final int LEFT = 0;
    public static final int TOP = 1;
    public static final int RIGHT = 2;
    public static final int BOTTOM = 3;

    @IntDef ({LEFT, TOP, RIGHT, BOTTOM})

    @Retention (RetentionPolicy.SOURCE)
    public @interface DrawGravity {
    }

    public ZKDrawableCenterTextView(Context context) {
        this(context, null);
    }

    public ZKDrawableCenterTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public ZKDrawableCenterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        drawables = new Drawable[4];
        drawableWidths = new int[4];
        drawableHeights = new int[4];
        setGravity(Gravity.CENTER);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DrawableCenterTextView);

        drawables[0] = array.getDrawable(R.styleable.DrawableCenterTextView_leftDrawable);
        drawables[1] = array.getDrawable(R.styleable.DrawableCenterTextView_topDrawable);
        drawables[2] = array.getDrawable(R.styleable.DrawableCenterTextView_rightDrawable);
        drawables[3] = array.getDrawable(R.styleable.DrawableCenterTextView_bottomDrawable);

        drawableWidths[0] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_leftDrawableWidth, 0);
        drawableWidths[1] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_topDrawableWidth, 0);
        drawableWidths[2] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_rightDrawableWidth, 0);
        drawableWidths[3] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_bottomDrawableWidth, 0);

        drawableHeights[0] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_leftDrawableHeight, 0);
        drawableHeights[1] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_topDrawableHeight, 0);
        drawableHeights[2] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_rightDrawableHeight, 0);
        drawableHeights[3] = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_bottomDrawableHeight, 0);

        if (drawables[0] != null && drawableWidths[0] <= 0) {
            drawableWidths[0] = drawables[0].getIntrinsicWidth();
        }
        if (drawables[1] != null && drawableWidths[1] <= 0) {
            drawableWidths[1] = drawables[1].getIntrinsicWidth();
        }
        if (drawables[2] != null && drawableWidths[2] <= 0) {
            drawableWidths[2] = drawables[2].getIntrinsicWidth();
        }
        if (drawables[3] != null && drawableWidths[3] <= 0) {
            drawableWidths[3] = drawables[3].getIntrinsicWidth();
        }

        if (drawables[0] != null && drawableHeights[0] <= 0) {
            drawableHeights[0] = drawables[0].getIntrinsicHeight();
        }
        if (drawables[1] != null && drawableHeights[1] <= 0) {
            drawableHeights[1] = drawables[1].getIntrinsicHeight();
        }
        if (drawables[2] != null && drawableHeights[2] <= 0) {
            drawableHeights[2] = drawables[2].getIntrinsicHeight();
        }
        if (drawables[3] != null && drawableHeights[3] <= 0) {
            drawableHeights[3] = drawables[3].getIntrinsicHeight();
        }

        array.recycle();
    }


    public void setDrawable(@DrawGravity int gravity, @Nullable Drawable drawable) {
        drawables[gravity] = drawable;
        if (drawable != null) {
            drawableWidths[gravity] = drawable.getIntrinsicWidth();
            drawableHeights[gravity] = drawable.getIntrinsicHeight();
        }
        postInvalidate();
    }

    public void setDrawable(@DrawGravity int gravity, Drawable drawable, int width, int height) {
        drawables[gravity] = drawable;
        drawableWidths[gravity] = width;
        drawableHeights[gravity] = height;
        postInvalidate();
    }


    public void setDrawables(Drawable[] drawables, int[] widths, int[] heights) {
        if (drawables != null && drawables.length >= 4
                && widths != null && widths.length >= 4
                && heights != null && heights.length >= 4) {
            this.drawables = drawables;
            this.drawableWidths = widths;
            this.drawableHeights = heights;
            postInvalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int drawablePadding = getCompoundDrawablePadding();
        translateText(canvas, drawablePadding);
        super.onDraw(canvas);

        float centerX = (getWidth() + getPaddingLeft() - getPaddingRight()) / 2;
        float centerY = (getHeight() + getPaddingTop() - getPaddingBottom()) / 2;

        float halfTextWidth = getPaint().measureText(getText().toString().isEmpty() ? getHint().toString() : getText().toString()) / 2;
        Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        float halfTextHeight = (fontMetrics.descent - fontMetrics.ascent) / 2;

        if (drawables[0] != null) {
            int left = (int) (centerX - drawablePadding - halfTextWidth - drawableWidths[0]);
            int top = (int) (centerY - drawableHeights[0] / 2);
            drawables[0].setBounds(left, top, left + drawableWidths[0], top + drawableHeights[0]);
            canvas.save();
            drawables[0].draw(canvas);
            canvas.restore();
        }

        if (drawables[2] != null) {
            int left = (int) (centerX + halfTextWidth + drawablePadding);
            int top = (int) (centerY - drawableHeights[2] / 2);
            drawables[2].setBounds(left, top, left + drawableWidths[2], top + drawableHeights[2]);
            canvas.save();
            drawables[2].draw(canvas);
            canvas.restore();
        }

        if (drawables[1] != null) {
            int left = (int) (centerX - drawableWidths[1] / 2);
            int bottom = (int) (centerY - halfTextHeight - drawablePadding);
            drawables[1].setBounds(left, bottom - drawableHeights[1], left + drawableWidths[1], bottom);
            canvas.save();
            drawables[1].draw(canvas);
            canvas.restore();
        }

        if (drawables[3] != null) {
            int left = (int) (centerX - drawableWidths[3] / 2);
            int top = (int) (centerY + halfTextHeight + drawablePadding);
            drawables[3].setBounds(left, top, left + drawableWidths[3], top + drawableHeights[3]);
            canvas.save();
            drawables[3].draw(canvas);
            canvas.restore();
        }
    }

    private void translateText(Canvas canvas, int drawablePadding) {

        int translateWidth = 0;
        if (drawables[0] != null && drawables[2] != null) {
            translateWidth = (drawableWidths[0] - drawableWidths[2]) / 2;
        } else if (drawables[0] != null) {
            translateWidth = (drawableWidths[0] + drawablePadding) / 2;
        } else if (drawables[2] != null) {
            translateWidth = -(drawableWidths[2] + drawablePadding) / 2;
        }

        int translateHeight = 0;
        if (drawables[1] != null && drawables[3] != null) {
            translateHeight = (drawableHeights[1] - drawableHeights[3]) / 2;
        } else if (drawables[1] != null) {
            translateHeight = (drawableHeights[1] + drawablePadding) / 2;
        } else if (drawables[3] != null) {
            translateHeight = -(drawableHeights[3] - drawablePadding) / 2;
        }
        canvas.translate(translateWidth, translateHeight);
    }

}
